import base64

import requests

saTokenB64 = base64.b64encode("cicd01@@@abcd".encode("utf-8"))
header = {
    "x-serviceAccount": saTokenB64
}

def create_ins():
    url = "http://localhost/api/k8sApp/createK8sInstanceOne"
    data = {
        "name": "rc05",
        "cluster": "bigdevops",
        # "image": "nginx:1.7.9",
        "image": "busybox",
        "k8SAppID": 3,
        "cpuRequest":"0.04",
        "labels":["k223=v3"],
    }
    # res = requests.post(url, json=data)
    res = requests.post(url, json=data,headers=header)
    print(res.status_code)
    print(res.text)
    print(res.json())




def get_ins_new():
    url = "http://localhost/api/k8sApp/getClusterNamespacesUnique"
    data = {
        "cluster": "bigdevops",
        "app":"k8s-app-0",

    }

    res = requests.get(url,headers=header)
    print(res.status_code)
    print(res.text)
    # print(res.json())

def update_ins():
    url = "http://localhost/noAuth/updateK8sInstanceOne"
    data = {
        "name": "busy01",
        "cluster": "bigdevops",
        "image": "busybox",
        "k8SAppID": 1,
        "id": 1,
        "cpuRequest":"0.07",

    }
    res = requests.post(url, json=data)
    print(res.status_code)
    print(res.text)
    print(res.json())

def delete_ins():
    url = "http://localhost/noAuth/batchDeleteK8sInstance"
    data = {
        "cluster": "bigdevops",
        "ids":[3,4],

    }
    res = requests.delete(url, json=data)
    print(res.status_code)
    print(res.text)
    print(res.json())

def delete_app():
    url = "http://localhost/noAuth/deleteK8sAppOne/1"
    data = {
        "cluster": "bigdevops",

    }
    res = requests.delete(url, params=data)
    print(res.status_code)
    print(res.text)
    print(res.json())

def restart_ins():
    url = "http://localhost/noAuth/batchRestartK8sInstance"
    data = {
        "cluster": "bigdevops",
        "ids":[3],

    }
    res = requests.post(url, json=data)
    print(res.status_code)
    print(res.text)
    print(res.json())


def get_ins():
    url = "http://localhost/noAuth/getK8sInstanceByApp"
    data = {
        "cluster": "bigdevops",
        "app":"k8s-app-0",

    }
    res = requests.get(url, params=data)
    print(res.status_code)
    print(res.text)
    # print(res.json())


def update_app():
    url = "http://localhost/noAuth/updateK8sAppOne"

    data = {
        "cluster": "bigdevops",
        "id": 1,
        "cpuRequest":"0.07",
        "labels": ["add=001"],
        "name":"k8s-app-0",
        "namespace":"default",
        "UserID":1,
        "treeNodeId":5,
        "portJson": '''[{"name":"http","protocol":"TCP","port":82,"targetPort":81}]''',
        "volumeJson": '''[{"type":"hostPath","name":"log","mountPath":"/log","subPath":"sub","pvcName":"","cmName":"","hostPathPath":"/data/k8s-app-0","hostPathType":"DirectoryOrCreate"},{"type":"configMap","name":"cm","mountPath":"/cm","subPath":"","pvcName":"","cmName":"cm-demo01","hostPathPath":"","hostPathType":""}]''',
        "commands": ["sh","-c","echo The app is running! && sleep infinity"],


    }
    res = requests.post(url, json=data)
    print(res.status_code)
    print(res.text)
    print(res.json())

create_ins()
# get_ins_new()
# update_ins()
# delete_ins()

# restart_ins()
# get_ins()
# update_app()
# delete_app()
# delete_ins()

